// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FlattenCategoriesConverter.cs" company="Akzente.IT">
//   (c) 2012 Akzente.IT
// </copyright>
// <summary>
//   Converts a node to its list of children with all categories ("Views", "Tables", etc.) flattened.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AkzenteIT.DatabaseDocumentationTool.Converter
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Windows.Data;

    using AkzenteIT.DatabaseDocumentationTool.ViewModels.Base;

    /// <summary>
    /// Converts a node to its list of children with all categories ("Views", "Tables", etc.) flattened.
    /// </summary>
    /// <remarks>
    /// For example: A table node [Products] has the logical children / categories "Views" and "Tables".
    /// This converter, given, the [Products] node, will return all table and views directly, thereby
    /// removing the category nodes. This is used so that we can display all items inside a DataGrid and
    /// use the DataGrid grouping functionality to display categories instead.
    /// </remarks>
    public class FlattenCategoriesConverter : IValueConverter
    {
        /// <summary>
        /// Converts a node to its list of children with all categories ("Views", "Tables", etc.) flattened.
        /// </summary>
        /// <param name="value">The value produced by the binding source.</param>
        /// <param name="targetType">The type of the binding target property.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var node = value as NodeViewModel;

            if (node == null)
            {
                return value;
            }

            return this.FlattenCategories(node.Children).ToList();
        }

        /// <summary>
        /// Converts a value.
        /// </summary>
        /// <param name="value">The value that is produced by the binding target.</param>
        /// <param name="targetType">The type to convert to.</param>
        /// <param name="parameter">The converter parameter to use.</param>
        /// <param name="culture">The culture to use in the converter.</param>
        /// <returns>
        /// A converted value. If the method returns null, the valid null value is used.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        /// <summary>
        /// Flattens the categories in a list of nodes.
        /// </summary>
        /// <param name="nodes">The nodes (possibly categories) which are inspected and flattened if needed.</param>
        /// <returns>The input nodes with each category node (IsCategory) replaced with its children.</returns>
        private IEnumerable<NodeViewModel> FlattenCategories(IEnumerable<NodeViewModel> nodes)
        {
            foreach (var node in nodes)
            {
                if (node.IsCategory)
                {
                    foreach (var child in node.Children)
                    {
                        yield return child;
                    }
                }
                else
                {
                    yield return node;
                }
            }
        }
    }
}